package com.xl.core.util.seg;

import java.util.LinkedHashSet;
import java.util.Set;

import org.apdplat.word.WordSegmenter;
import org.apdplat.word.segmentation.SegmentationAlgorithm;
import org.apdplat.word.segmentation.Word;

/**
 * 
 * word分词器
 * 
 * @author 梁华
 * 
 */
public class WordSegmenterUtils {

	public static Set<String> segValues(String text) {

		// 返回值
		Set<String> valueSet = new LinkedHashSet<String>();

		// 双向最大最小匹配算法：BidirectionalMaximumMinimumMatching
		for (Word word : WordSegmenter.segWithStopWords(text,
				SegmentationAlgorithm.BidirectionalMaximumMinimumMatching)) {
			valueSet.add(word.getText());
		}

		// 双向最大匹配算法：BidirectionalMaximumMatching
		for (Word word : WordSegmenter.segWithStopWords(text,
				SegmentationAlgorithm.BidirectionalMaximumMatching)) {
			valueSet.add(word.getText());
		}

		// 双向最小匹配算法：BidirectionalMinimumMatching
		for (Word word : WordSegmenter.segWithStopWords(text,
				SegmentationAlgorithm.BidirectionalMinimumMatching)) {
			valueSet.add(word.getText());
		}

		// 正向最大匹配算法：MaximumMatching
		for (Word word : WordSegmenter.segWithStopWords(text,
				SegmentationAlgorithm.MaximumMatching)) {
			valueSet.add(word.getText());
		}

		// 正向最小匹配算法：MinimumMatching
		for (Word word : WordSegmenter.segWithStopWords(text,
				SegmentationAlgorithm.MinimumMatching)) {
			valueSet.add(word.getText());
		}

		// 逆向最小匹配算法：ReverseMinimumMatching
		for (Word word : WordSegmenter.segWithStopWords(text,
				SegmentationAlgorithm.ReverseMinimumMatching)) {
			valueSet.add(word.getText());
		}
		// 逆向最大匹配算法：ReverseMaximumMatching
		for (Word word : WordSegmenter.segWithStopWords(text,
				SegmentationAlgorithm.ReverseMaximumMatching)) {
			valueSet.add(word.getText());
		}

		return valueSet;
	}

	public static void main(String[] args) {
		Set<String> valueSet = WordSegmenterUtils
				.segValues("村里有个姑娘叫小芳，长的好看又漂亮");
		for (String str : valueSet) {
			System.out.println(str);
		}
	}
}
